﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Member_MyOrder_OrderDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null || Session["AdminUserID"] != null)
            {
                BindData();
                VisibleButton();
            }
            else
            {
                Response.Write("<script language=javascript>window.close();</script>");
                return;
            }

          

        }
    }




    private void VisibleButton()
    {
        string State = Request.QueryString["State"];
        if (State == "Y")
        {
            lblTitle.Text = "ĐƠN ĐANG XÁC NHẬN";


        }
        else if (State == "C")
        {
            lblTitle.Text = "ĐƠN ĐÃ HỦY";

        }
        else if (State == "S")
        {
            lblTitle.Text = "ĐƠN ĐANG GIAO HÀNG";   

        }
        else if (State == "E")
        {
            lblTitle.Text = "ĐƠN ĐÃ GIAO HÀNG";


        }
        else
        {
            lblTitle.Text = "ĐƠN CHỜ XÁC NHẬN";

        }

    }
    private void BindData()
    {
        clsWebShop cls = new clsWebShop();
        clsCommon clsCom = new clsCommon();
        //Lay so luong trong kho
        //Session["dtCountStore"] = cls.Get_QuantityStoreGroup("", "0").Tables[0];
        DataTable dt;
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("StartDate", "");
        msg.CreateFirstNode("EndDate", "");
        msg.CreateFirstNode("Status", cls.killSQLInjection(Request.QueryString["State"]));
        msg.CreateFirstNode("End_MK", "");
        msg.CreateFirstNode("OrderID", cls.killSQLInjection(Request.QueryString["OrderID"]));
        dt = cls.Get_OrderPDByCondition(msg.GetXmlStr.ToString()).Tables[0];


        if (dt.Rows.Count > 0)
        {
            //lblMaDH.Text = dt.Rows[0]["OrderID"].ToString();
            lblMaDH.Text=dt.Rows[0]["OrderID"].ToString().Substring(4, dt.Rows[0]["OrderID"].ToString().Length - 4);
            lblTenKH.Text = dt.Rows[0]["CusNm"].ToString();
            // lblTaxNo.Text = clsCom.FormatDate(dt.Rows[0]["BirthDay"].ToString());
            lblDiaChi.Text = dt.Rows[0]["Address"].ToString();
            lblNoichon.Text = dt.Rows[0]["PlaceNm"].ToString();
            lblSDT.Text = dt.Rows[0]["Phone"].ToString();
            lblEmail.Text = dt.Rows[0]["Email"].ToString();
            lblTaxNo.Text = dt.Rows[0]["TaxNo"].ToString();
            if (lblTaxNo.Text.Trim() == "")
            {
                trTaxNo.Visible = false;
            }

            grvMaster.DataSource = dt;
            grvMaster.DataBind();
            decimal TotalQuantity = 0;
            decimal TotalPrice = 0;
            for (int i = 0; i < grvMaster.Rows.Count; i++)
            {
                TotalQuantity += decimal.Parse(grvMaster.Rows[i].Cells[3].Text.ToString());
                TotalPrice += decimal.Parse(grvMaster.Rows[i].Cells[5].Text.ToString());
            }
            lblTongsosach.Text = FormatMoney(TotalQuantity);
            lblTongsotien.Text = FormatMoney(TotalPrice);


            lblUSD.Text = "<b>Tỉ giá USD:</b> " + FormatMoney(dt.Rows[0]["USD"].ToString()) + " VNĐ";


        }
    }


    protected void grvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (DataBinder.Eval(e.Row.DataItem, "KindNo").ToString() != "P")
            {
                e.Row.BackColor = System.Drawing.Color.FromName("LightGoldenrodYellow");
            }
            e.Row.Cells[4].Text = FormatMoney(e.Row.Cells[4].Text) + " " + DataBinder.Eval(e.Row.DataItem, "Rate").ToString();
            e.Row.Cells[5].Text = FormatMoney(e.Row.Cells[5].Text);
        }
    }



    protected void Button5_Click(object sender, EventArgs e)
    {

    }
    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Write("<script language=javascript>window.close();</script>");
    }
}
